package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.TaskAttachmentsDao;
import com.rstrulak.dao.TaskDao;
import com.rstrulak.model.Task;
import com.rstrulak.model.TaskAttachments;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("taskBean")
@Scope("request")
public class TaskBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5054393997565623065L;

	@Autowired
	private TaskDao taskDao;
	
	@Autowired
	private TaskAttachmentsDao taskAttachmentsDao;

	private Task task;

	@SuppressWarnings("unused")
	private Boolean hasProject;

	@SuppressWarnings("unused")
	private Boolean hasContext;

	@SuppressWarnings("unused")
	private Boolean taskOwner;

	private Long taskId;
	
	private List<TaskAttachments> taskAttachmentsList;

	@PostConstruct
	public void init() {
		taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		
		if(taskId == null){
			JSFUtils.setFlashFromSession();
			taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
			JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		}

		JSFUtils.reloadFlash();
		getTaskFromDb(taskId);
		
		if (task!= null) {
			taskAttachmentsList= taskAttachmentsDao.getTaskAttachemntsListByTask(task);
		}
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}

	public Boolean getHasProject() {
		return (task.getProject() != null);
	}

	public void setHasProject(Boolean hasProject) {
		this.hasProject = hasProject;
	}

	public Boolean getHasContext() {
		return (task.getContext() != null);
	}

	public void setHasContext(Boolean hasContext) {
		this.hasContext = hasContext;
	}
	
	public List<TaskAttachments> getTaskAttachmentsList() {
		return taskAttachmentsList;
	}

	public void setTaskAttachmentsList(List<TaskAttachments> taskAttachmentsList) {
		this.taskAttachmentsList = taskAttachmentsList;
	}

	public Boolean getTaskOwner() {
		return ((JSFUtils.getLoggedUser().getId()).equals(task.getOwner().getId()));
	}

	public void setTaskOwner(Boolean isTaskOwner) {
		this.taskOwner = isTaskOwner;
	}

	private void getTaskFromDb(Long taskId) {
		task = taskDao.getById(taskId);
	}

	public void goToEditTaskSite() throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		JSFUtils.redirect("/protected/editTask.xhtml");
	}

	public void showTaskDetails(Long taskId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		JSFUtils.redirect("/protected/taskDetails.xhtml");
	}

}
